package com.xiashitech.agent.instrumentation.dubbo.inst;

import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.*;
import com.xiashitech.agent.instrumentation.dubbo.common.inst.DubboTelemetry;
import com.xiashitech.agent.instrumentation.dubbo.common.inst.internal.DubboNetClientAttributesGetter;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
//import io.opentelemetry.javaagent.shaded.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
import io.opentelemetry.javaagent.shaded.instrumentation.api.instrumenter.net.PeerServiceAttributesExtractor;
import io.opentelemetry.javaagent.shaded.io.opentelemetry.api.GlobalOpenTelemetry;

@Activate(group = {"provider"})
//@Activate(group = {"consumer", "provider"})
public class XSDubboFilter implements Filter {

    private final Filter delegate;

    public XSDubboFilter() {
        delegate =
                DubboTelemetry.builder(GlobalOpenTelemetry.get())
                        .addAttributesExtractor(
                                PeerServiceAttributesExtractor.create(
                                        new DubboNetClientAttributesGetter(),
                                        CommonConfig.get().getPeerServiceMapping()))
                        .addAttributesExtractor(new XSDubboAttributesExtractor())
                        .build()
                        .newFilter();
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        return delegate.invoke(invoker, invocation);
    }
}
